<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :before-close="cancel" :visible.sync="dialog" :title="title" width="900px">
    <el-form v-show="hidden == false" ref="form" :model="form" :inline="true" :rules="rules" label-width="80px">
      <el-form-item label="商品图片">
       <!-- <el-upload
          :action="uploadUrl"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :on-success="handleSuccess"
          list-type="picture">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload> -->
        <template>
         <div v-for="image in imgs" :key="image.index" class="scale-down" style="float: left;padding-left: 5px;">
          <el-image
            style="width: 148px; height: 148px"
            :src="image">
            </el-image>
            <i class="el-icon-delete del" v-on:click="remove(image)"></i>
        </div>

        <el-upload
            :action="uploadUrl"
            list-type="picture-card"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-success="handleSuccess">
            <i class="el-icon-plus"></i>
          </el-upload>
          <div>
          <!-- <el-dialog visible.sync="false">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog> -->
          </div>
           </template>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>

</template>

<script>
import { getCates } from '@/api/StoreCategory'
import { relatedImg, edit, isFormatAttr, setAttr, clearAttr, getImg } from '@/api/StoreProduct'
import editor from '../../components/Editor'
import picUploadTwo from '@/components/pic-upload-two'
import mulpicUpload from '@/components/mul-pic-upload'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { Message } from 'element-ui'
export default {
  components: { editor, picUploadTwo, mulpicUpload, Treeselect },
  props: {
    isAttr: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      loading: false, dialog: false, cates: [], title: '关联图片',
      form: {
        id:'',
        goodsName: '',
        costPrice:0,
        sellPrice: 0,
        inventory:0,
        status:1
      },
      uploadUrl:'http://localhost:8888/api/upload',
      dialogImageUrl: '',
      dialogVisible: false,
      imgs:[],
      editType:false,
      rules: {
      },
      items: [{
        value: '',
        detailValue: ''
        // attrHidden: false,
        // detail: []
      }],
      attrs: [],
      hidden: false,
      attrHidden: false,
      submiting: false
    }
  },
  methods: {
    getImg(id) {
      getImg(id).then(res => {
        console.log('res' + res.data)

        this.imgs=res.data
        console.log('img' + this.imgs)
      })
    },
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.loading = true
        this.doAdd()
    },
    doAdd() {
      console.log(this.form,"form")
      console.log(this.imgs,"img")
      //如果后端有专门的图片表，则封装为一个对象，没有则封装为map
      const map={"goodsId":this.form.id,"img":this.imgs}
      relatedImg(map).then(res => {
        this.resetForm()
        this.$notify({
          title: '关联成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    handleRemove(file, fileList) {
        console.log(file, fileList);
        //应从img中删除
      },
      remove(image){
        const len=(this.imgs).indexOf(image)
        let imgArr=this.imgs;
        imgArr.splice(len,1)
      },
      handlePreview(file) {
        // this.dialogImageUrl = file.url;
        // this.dialogVisible = true;
      },
      handleSuccess(res){
        console.log(res.data)
        this.imgs.push(res.data)
      },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id:'',
        goodsName: '',
        costPrice:0,
        sellPrice: 0,
        inventory:0,
        status:1
      },
      this.imgs=[]
    }
  }
}
</script>

<style scoped>

  .demo-upload{
    display: block;
    /*//height: 50px;*/
    text-align: center;
    border: 1px solid transparent;
    border-radius: 4px;
    overflow: hidden;
    background: #fff;
    position: relative;
    box-shadow: 0 1px 1px rgba(0,0,0,.2);
    margin-right: 4px;
  }
  .demo-upload img{
    width: 100%;
    height: 100%;
    display: block;
  }

  .demo-upload-cover{
    display: block;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,.6);
  }
  .demo-upload:hover .demo-upload-cover{
    display: block;
  }
  .demo-upload-cover i{
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    margin: 0 2px;
  }
.del{
    cursor: pointer;
    margin-left: -20px;
    float: right;
    background: red;
    color: #fff;
    font-size: 10px;
    padding: 4px 4px;
    position: relative;
    right: 0px;
    z-index: 9;
    border-radius: 3px;
    }

</style>
